package com.qj.platform.pay.core.dao;

import com.qj.base.commons.domain.SearchObject;
import com.qj.base.commons.utils.CodeGenerator;
import com.qj.base.commons.utils.SqlUtil;
import com.qj.platform.pay.core.domain.Agent;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhijie on 2016/12/15.
 */
@Repository
public class AgentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Agent getById(String id){
        List<Agent> orderList=jdbcTemplate.query("select * from agent where id=?",new Object[]{id},new AgentRowMapper());
        if(orderList.size()>0){
            return orderList.get(0);
        }
        return null;
    }

    public List<Agent> search(SearchObject searchObject){
        List<Object> argList=new ArrayList<>();
        return jdbcTemplate.query(SqlUtil.getSearchSql(argList,"agent",searchObject),argList.toArray(new Object[]{}),new AgentRowMapper());
    }
    public int searchCount(SearchObject searchObject){
        List<Object> argList=new ArrayList<>();
        return jdbcTemplate.queryForObject(SqlUtil.getCountSql(argList,"agent",searchObject),argList.toArray(new Object[]{}),Integer.class);
    }


    public String insert(Agent agent){
        StringBuilder sql=new StringBuilder("insert into agent(");
        StringBuilder values=new StringBuilder();
        List<Object> argList=new ArrayList<Object>();
        if(StringUtils.isNotEmpty(agent.getTel())){
            argList.add(agent.getTel());
            sql.append("tel,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(agent.getEmail())){
            argList.add(agent.getEmail());
            sql.append("email,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(agent.getRemark())){
            argList.add(agent.getRemark());
            sql.append("remark,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(agent.getLevel())){
            argList.add(agent.getLevel());
            sql.append("level,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(agent.getSuperAgentId())){
            argList.add(agent.getSuperAgentId());
            sql.append("super_id,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(agent.getNickName())){
            argList.add(agent.getNickName());
            sql.append("nickname,");
            values.append("?,");
        }
        if(agent.getAgentCommissionRate()!=null&& agent.getAgentCommissionRate()>0){
            argList.add(agent.getAgentCommissionRate());
            sql.append(",agent_commission_rate");
            values.append("?,");
        }
        if(agent.getAgentT0TradeRate()!=null&& agent.getAgentT0TradeRate()>0){
            argList.add(agent.getAgentT0TradeRate());
            sql.append(",agent_t0_trade_rate");
            values.append("?,");
        }
        if(agent.getMerchantCommissionRate()!=null&& agent.getMerchantCommissionRate()>0){
            argList.add(agent.getMerchantCommissionRate());
            sql.append(",merchant_commission_rate");
            values.append("?,");
        }
        if(agent.getMerchantT0TradeRate()!=null&& agent.getMerchantT0TradeRate()>0){
            argList.add(agent.getMerchantT0TradeRate());
            sql.append(",merchant_t0_trade_rate");
            values.append("?,");
        }
        sql.append("create_time,");
        values.append("now(),");
        sql.append("modify_time,");
        values.append("now(),");

        agent.setId(CodeGenerator.getUUID32());
        argList.add(agent.getId());
        sql.append("id");
        values.append("?");

        sql.append(") values(").append(values).append(")");
        jdbcTemplate.update(sql.toString(),argList.toArray(new Object[]{}));
        return agent.getId();
    }

    public void update(Agent agent){
        List<Object> argList=new ArrayList<>();
        StringBuilder sql=new StringBuilder("update agent set modify_time=now()");

        if(StringUtils.isNotEmpty(agent.getTel())){
            argList.add(agent.getTel());
            sql.append(",tel=?");
        }
        if(StringUtils.isNotEmpty(agent.getEmail())){
            argList.add(agent.getEmail());
            sql.append(",email=?");
        }
        if(StringUtils.isNotEmpty(agent.getRemark())){
            argList.add(agent.getRemark());
            sql.append(",remark=?");
        }
        if(StringUtils.isNotEmpty(agent.getLevel())){
            argList.add(agent.getLevel());
            sql.append(",level=?");
        }
        if(StringUtils.isNotEmpty(agent.getSuperAgentId())){
            argList.add(agent.getSuperAgentId());
            sql.append(",super_id=?");
        }
        if(StringUtils.isNotEmpty(agent.getNickName())){
            argList.add(agent.getNickName());
            sql.append(",nickname=?");
        }
        if(agent.getAgentCommissionRate()!=null&& agent.getAgentCommissionRate()>0){
            argList.add(agent.getAgentCommissionRate());
            sql.append(",agent_commission_rate=?");
        }
        if(agent.getAgentT0TradeRate()!=null&& agent.getAgentT0TradeRate()>0){
            argList.add(agent.getAgentT0TradeRate());
            sql.append(",agent_t0_trade_rate=?");
        }
        if(agent.getMerchantCommissionRate()!=null&& agent.getMerchantCommissionRate()>0){
            argList.add(agent.getMerchantCommissionRate());
            sql.append(",merchant_commission_rate=?");
        }
        if(agent.getMerchantT0TradeRate()!=null&& agent.getMerchantT0TradeRate()>0){
            argList.add(agent.getMerchantT0TradeRate());
            sql.append(",merchant_t0_trade_rate=?");
        }
        sql.append(" where id=?");
        argList.add(agent.getId());
        jdbcTemplate.update(sql.toString(),argList.toArray(new Object[]{}));
    }

    class AgentRowMapper implements RowMapper<Agent> {
        @Override
        public Agent mapRow(ResultSet resultSet, int i) throws SQLException {
            Agent agent=new Agent();
            agent.setId(resultSet.getString("id"));
            agent.setSuperAgentId(resultSet.getString("super_id"));
            agent.setNickName(resultSet.getString("nickname"));
            agent.setLevel(resultSet.getString("level"));
            agent.setTel(resultSet.getString("tel"));
            agent.setRemark(resultSet.getString("remark"));
            agent.setAgentCommissionRate(resultSet.getDouble("agent_commission_rate"));
            agent.setCreateTime(resultSet.getTimestamp("create_time"));
            agent.setModifyTime(resultSet.getTimestamp("modify_time"));
            agent.setAgentT0TradeRate(resultSet.getDouble("agent_t0_trade_rate"));
            agent.setMerchantCommissionRate(resultSet.getDouble("merchant_commission_rate"));
            agent.setMerchantT0TradeRate(resultSet.getDouble("merchant_t0_trade_rate"));
            return agent;
        }
    }
}
